﻿/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4-vsdoc.js" />

/*
JavaScript functions for the TVGuide.aspx page
*/

//localized strings
var datepickerButtonText = new Array();
datepickerButtonText[''] = 'Click here to choose a date';
datepickerButtonText['de'] = 'Klicken Sie hier, um ein Datum auszuwählen';

$(document).ready(function () {

    //get the page request manager
    var pageReqMgr = Sys.WebForms.PageRequestManager.getInstance();

    //add request for invoking the functions when UpdatePanel has finished loading
    pageReqMgr.add_endRequest(UpdateTooltips);
    pageReqMgr.add_endRequest(UpdateDatepicker);
    pageReqMgr.add_endRequest(UpdateSearchbox);

    //add request for disabling content when UpdatePanel begins loading
    pageReqMgr.add_beginRequest(DisableContent);

    //setup the programInfoDialog
    InitProgramInfoDialog();
});


function DisableContent() {

    // disable all input and select elements
    $('input').attr('disabled', 'disabled');
    $('select').attr('disabled', 'disabled');

    // fade out the programs div
    $('#TVGuidePrograms').fadeTo('fast', 0.33);
}

function UpdateSearchbox() {

    try {
        var searchbox = $('input[id$=txtSearch]:first');

        searchbox.autocomplete({
            source: "SearchTVPrograms.ashx?searchmode=3",
            minLength: 2
        });
    }
    catch (e) {
        alert(e);
    }
}

function UpdateDatepicker() {

    try {
        var hiddenfielddate = $('input[id$=HiddenFieldDate]:first');

        //associate a datepicker with the hidden field
        hiddenfielddate.datepicker(
        {
            dateFormat: 'yy-mm-dd',
            buttonImage: 'style/images/TVGuide/Calendar.gif',
            buttonImageOnly: true,
            showOn: 'both',
            showAnim: 'slideDown',
            duration: 'slow'
        });

        //localize datepicker
        if ($.datepicker.regional[lang] != undefined)
            hiddenfielddate.datepicker('option', $.datepicker.regional[lang]);
        else
            hiddenfielddate.datepicker('option', $.datepicker.regional['']);

        if (datepickerButtonText[lang] != undefined)
            hiddenfielddate.datepicker('option', 'buttonText', datepickerButtonText[lang]);
        else
            hiddenfielddate.datepicker('option', 'buttonText', datepickerButtonText['']);

        //initiate postback if the value of the hidden field changes (-> when the calender date is changed)
        hiddenfielddate.change(function () {
            __doPostBack(hiddenfielddate.attr('id'), '');
        });

        // show the datepicker when the user clicks on the date label
        $('span[id$=lblDate]:first').click(function () {
            hiddenfielddate.datepicker('show');
        });

    }
    catch (e) {
        alert(e);
    }
}

function UpdateTooltips() {

    try {
        //attach tooltips to the program cells that have a programID
        $('div.TVGuideCellProgram[programID],div.TVGuideCellProgramOnAir[programID],div.TVGuideCellProgramOnScheduled[programID]').tooltip({
            effect: 'slide',
            direction: 'down',
            tip: '#programShortInfoTooltip',
            position: 'bottom center',
            predelay: 300,
            lazy: false,
            offset: [-20, 0],
            onBeforeShow: function () {
                //get reference to the jQuery object
                var contentWrapper = $('#programShortInfoTooltipContentWrap');

                //hide the content wrapper
                contentWrapper.hide();

                //load the short info page into the wrapper. When the loading is finished, show the content wrapper.
                contentWrapper.load('ProgramShortInfo.aspx', { id: this.getTrigger().attr('programID') }, function (responseText, textStatus, XMLHttpRequest) {
                    contentWrapper.fadeIn();
                });
            }
        });
    }
    catch (e) {
        alert(e);
    }
}

function InitProgramInfoDialog() {

    try {
        //make the blanket opaque
        $('#programInfoDialogBlanket').css('opacity', '0.7');

        //initialize the program details popup
        $('#programInfoDialog').overlay({
            closeOnClick: false, //user must use close button or ESC to close overlay
            api: true, //returns an api object when accessing overlay() after initialization. We need this for the showProgramInfoDialog() function.
            onBeforeLoad: function () {
                $('#programInfoDialogBlanket').height($(document).height()); //span the blanket to the height of the document
                $('#programInfoDialogBlanket').fadeIn();
            },
            onClose: function () {
                $('#programInfoDialogBlanket').fadeOut();
            }
        });

        //make the popup resizable
        $('#programInfoDialog').resizable({
            handles: 's, se, w',
            start: function (event, ui) {
                //show a mask over the iFrame to prevent IE from stealing mouse events
                $("#programInfoDialogiFrameFixMask").show();
            },
            stop: function (event, ui) {
                //hide the mask when dragging ends
                $("#programInfoDialogiFrameFixMask").hide();
            }
        });
    }
    catch (e) {
        alert(e);
    }
}

function showProgramInfoDialog(programID) {

    try {
        //load the content of the overlay into the wrapper, via an iFrame
        $('#programInfoDialogContentWrap').empty();
        $('#programInfoDialogContentWrap').append('<iframe id="programInfoDialogiFrame" frameborder="0" height="100%" width="100%" src="ProgramInfoDialog.aspx?embedded=true&id=' + programID + '"></iframe>');

        //show the overlay
        $('#programInfoDialog').overlay().load();
    }
    catch (e) {
        alert(e);
    }
}